package com.chamei.compiler;

import com.chamei.annotation.ARouter;
import com.chamei.compiler.utils.Constants;
import com.google.auto.service.AutoService;

import java.io.IOException;
import java.io.Writer;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;


@AutoService(Processor.class)
// 接收的类型
@SupportedAnnotationTypes({Constants.AROUTER_ANNOTATION_TYPES})
// JDK的版本
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class ARouterProcessor extends AbstractProcessor {
    // 操作Element工具类
    private Elements elementsUtils;
    // 打印日志
    private Messager messager;
    // 文件生成器
    private Filer filer;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnvironment) {
        super.init(processingEnvironment);
        // 初始化工作
        elementsUtils = processingEnvironment.getElementUtils();
        messager = processingEnvironment.getMessager();
        filer = processingEnvironment.getFiler();
    }

// 可以用注解来代替

//    @Override
//    public Set<String> getSupportedAnnotationTypes() {
//        return super.getSupportedAnnotationTypes();
//        // 支持的注解类型
//    }
//
//    @Override
//    public SourceVersion getSupportedSourceVersion() {
//        return super.getSupportedSourceVersion();
//        // 支持的JDK版本
//    }
//
//
//    @Override
//    public Set<String> getSupportedOptions() {
//        return super.getSupportedOptions();
//        // 接受参数
//    }

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        if(set.isEmpty()) return false;
        // 获取所有带有ARouter注解的类节点
        Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(ARouter.class);
        if(elements.isEmpty()) return false;
        for(Element element:elements){
            // 通过类节点，获取包节点全路径名
            String packageName = elementsUtils.getPackageOf(element).getQualifiedName().toString();
            // 获取简单类名
            String className = element.getSimpleName().toString();
            messager.printMessage(Diagnostic.Kind.NOTE,"被注解的类："+className);
            // 最终生成类的名称
            String finalClassName = className + "$$ARouter";
            try{
                JavaFileObject sourceFile = filer.createSourceFile(packageName+"."+finalClassName);
                Writer writer = sourceFile.openWriter();
                writer.write("package "+packageName+";\n");
                writer.write("import android.util.Log;\n");
                writer.write("public class "+finalClassName+" {\n");
                writer.write("public void hello (String path){\n");
                writer.write("Log.d(\"aaa\",\"path:\"+path);\n");
                writer.write("}\n");
                writer.write("}\n");
                writer.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return true;
    }
}
